import fatReq from './req'

//示例 beforeHook和afterHook
const instance = fatReq.create();

instance.hooks.before = function(cfg){
  if(cfg.url.includes('/nccloud/api')){
    try {
      let user = localStorage.getItem('user');
      user = JSON.parse(user);

      let nccHeaders = {
        access_token: user.access_token,
        ucg_flag: user.ucg_flag,
        client_id: user.client_id
      }
      cfg.headers = new Headers({
        ...nccHeaders,
        ...cfg.headers,
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      });
    } 
    catch (err) {
      console.log(err)
    }  
  }
  else if(cfg.url.includes('/uapws')){
    cfg.headers = new Headers({
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    });
  }
  return cfg;
}

instance.hooks.after = function(data){
  if(data.code == 401 || data.code == 403){
    //处理未授权
  }
  return data;
}